# !/usr/bin/env python
# -*- coding: utf-8 -*-

import json
# @File   : fetchreformat.py
# @Time   : 2021/10/22 2:56 下午
# @Author : yujianhui
import re


# chrome开发者工具 copy as fetch的url 格式化
def fetch_url_format(fetch):
    http = re.search('fetch\("(.*?)",', fetch, re.IGNORECASE)
    url = http.group(1)

    json_str = fetch[fetch.index('{'):fetch.rindex('}') + 1]
    json_str = json.loads(json_str)

    method = json_str["method"]
    headers = json_str["headers"]
    body_str = json_str["body"]

    body = {}
    values = re.findall('"\r\n\r\n(.*?)\r\n', body_str)
    keys = re.findall('name="(.*?)"', body_str)

    if len(keys) == len(values):
        for i in range(len(values)):
            body[keys[i]] = values[i]
    else:
        print("解析body失败")

    req = {}
    req['body'] = body
    req['url'] = url
    req['method'] = method
    req['headers'] = headers

    return json.dumps(req, indent=2, ensure_ascii=False).replace("&", "&amp;")


if __name__ == '__main__':
    fetch = r"""
    fetch("http://34234/gateway/ovo-dos/smart1cpMonitor/getInfoCount", {
      "headers": {
        "accept": "application/json, text/plain, */*",
        "accept-language": "zh-CN,zh;q=0.9",
        "content-type": "multipart/form-data; boundary=----WebKitFormBoundary7gh5vdKo06OsyGfI"
      },
      "referrer": "http://ovo.chinawayltd.com/dos/",
      "referrerPolicy": "strict-origin-when-cross-origin",
      "body": "------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"pageNo\"\r\n\r\n1\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"useLevel\"\r\n\r\n\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"trucknos\"\r\n\r\n\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"saleNames\"\r\n\r\n\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"gpsnos\"\r\n\r\n\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"imeis\"\r\n\r\n\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"customers\"\r\n\r\n\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"statues\"\r\n\r\n\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"isBlack\"\r\n\r\n\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"date\"\r\n\r\n2021-10-21\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"isErr\"\r\n\r\n\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"isInsurance\"\r\n\r\n\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"isActive\"\r\n\r\n\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"labels\"\r\n\r\n\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI\r\nContent-Disposition: form-data; name=\"pageSize\"\r\n\r\n10\r\n------WebKitFormBoundary7gh5vdKo06OsyGfI--\r\n",
      "method": "POST",
      "mode": "cors",
      "credentials": "include"
    });
    """

    print(fetch_url_format(fetch))
